﻿using Framework.Common;
using Framework.Common.Exceptions;
using Framework.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Web;
using System.Web.Http.Filters;
using YouTravel.Framework.Enums;

namespace Application.WebAPI.Filter
{
    public class PageAccessFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var allowAnonymous = actionContext.ActionDescriptor.GetCustomAttributes<System.Web.Http.AllowAnonymousAttribute>();
            if (allowAnonymous.Any())
                return;
            //actionContext.Request.RequestUri.;

            ClaimsPrincipal principal = Thread.CurrentPrincipal as ClaimsPrincipal;
            string userId = principal.Claims.Single(c => c.Type == "sub").Value;
            var user = RedisHelp.GetLoginUserCache(int.Parse(userId));

            var actionDesc = actionContext.ActionDescriptor.GetCustomAttributes<AuthorityVerifyAttribute>();
            var isAccess = false;
            if (actionDesc.Any())
            {
                foreach (var item in actionDesc)
                {
                    //item.ValueArry.Any(v => (int)v == (int)user.Type);
                    foreach (AuthorityRole role in item.ValueArry)
                    {
                        if (role == AuthorityRole.Company && user.Type == UserType.Company)
                        {
                            isAccess = true;
                            break;
                        }
                        if (role == AuthorityRole.Branch && user.Type == UserType.Branch)
                        {
                            isAccess = true;
                            break;
                        }
                        if (role == AuthorityRole.SpecialLineAdministrator && user.Type == UserType.SpecialLine && user.IsSpecialManager)
                        {
                            isAccess = true;
                            break;
                        }
                        if (role == AuthorityRole.SpecialLineOperator && user.Type == UserType.SpecialLine && !user.IsSpecialManager)
                        {
                            isAccess = true;
                            break;
                        }
                        if (user.Type != 0) continue;
                        isAccess = true;
                        break;
                    }
                }
            }
            else
            {
                isAccess = true;
            }
            if (!isAccess)
            {
                throw new AuthorizeException("无此接口访问权限");
            }
            base.OnActionExecuting(actionContext);
        }
        public override System.Threading.Tasks.Task OnActionExecutingAsync(System.Web.Http.Controllers.HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            return base.OnActionExecutingAsync(actionContext, cancellationToken);
        }
    }
}